--
-- Author: dbliu
-- Date: 2014-02-28 21:19:26
--

local bgs_length=2

local MapLayer = class("MapLayer", function()
	return display.newNode()
end)

function MapLayer:ctor(filename)

	

	self.bgs={}

	for i=1,bgs_length do
		local bg = display.newSprite(filename, 0, (i-1)*960):addTo(self)
		bg:setScale(1.25)
		bg:setAnchorPoint(ccp(0, 0))

		self.bgs[i]=bg
	end

    -- create physics world
    self.world = CCPhysicsWorld:create()
    -- add world to scene
    self:addChild(self.world)

    app.world=self.world

    -- add debug node
    --self.worldDebug = app.world:createDebugNode()
    --self:addChild(self.worldDebug)

   	-- 注册帧事件
    self:addScriptEventListener(cc.Event.ENTER_FRAME, handler(self, self.tick))


end

function MapLayer:tick(dt)

	self:checkLifeCycle()

	
end

function MapLayer:checkLifeCycle()
	for i=1,bgs_length do
		local bg = self.bgs[i]
		local p = bg:convertToWorldSpace(ccp(0, 0))
		if p.y < -960 then
			local x,y = bg:getPosition()
			bg:setPosition(ccp(x,y+bgs_length*960))
		end
	end
end

return MapLayer